﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DataLibrary
{
    public class Ctbl_User:IData<tbl_User>
    {
        public Ctbl_User(String myconnecString) : base(myconnecString) { }


        protected override int InsertOnSubmit(tbl_User item, int t)
        {
            return connec.ExecuteNonQuery("ProUser", CommandType.StoredProcedure, new SqlParameter("@flag", t),
                new SqlParameter("@"+tbl_User.c_emailUser,item.EmailUser),new SqlParameter("@"+tbl_User.c_idUser,item.IdUser),
                new SqlParameter("@"+tbl_User.c_lastLoginUser,item.LastLoginUser),new SqlParameter("@"+tbl_User.c_nameUser,item.NameUser),
                new SqlParameter("@"+tbl_User.c_passwordUser,item.PasswordUser),new SqlParameter("@"+tbl_User.c_isActive,item.IsActive));
        }

        public override List<tbl_User> GetAll(tbl_User item)
        {
            throw new NotImplementedException();
        }

        public override tbl_User GetItemById(object id)
        {
            return GetAll().SingleOrDefault(p=>p.IdUser==Convert.ToInt32(id));
        }

        public override tbl_User GetItem(IDataReader dr)
        {
            tbl_User obj = new tbl_User();
            obj.EmailUser = dr[tbl_User.c_emailUser].ToString();
            obj.IdUser = dr[tbl_User.c_idUser] == null ? 1 : Convert.ToInt32(dr[tbl_User.c_idUser]);
            obj.NameUser = dr[tbl_User.c_nameUser].ToString();
            obj.PasswordUser = dr[tbl_User.c_passwordUser].ToString();
            obj.IsActive = Convert.ToInt32(dr[tbl_User.c_isActive]);
            return obj;
        }

        public override List<tbl_User> GetAll()
        {
            return GetAll("select * from "+tbl_User.b_tbl_User,CommandType.Text);
        }

        public int CheckLogin(string m_UserName, string m_PassWord) {
            SqlConnection connection = connec.Connection;
            SqlCommand command = new SqlCommand();
            command.CommandType =CommandType.StoredProcedure;
            command.CommandText = "Admin_CheckLogin";
            command.Connection = connection;
            command.Parameters.Add("@UserName", SqlDbType.NVarChar);
            command.Parameters["@UserName"].Value = m_UserName;
            command.Parameters.Add("@PassWord", SqlDbType.NVarChar);
            command.Parameters["@PassWord"].Value = m_PassWord;
            command.Parameters.Add("@result", SqlDbType.Int);
            command.Parameters["@result"].Direction = ParameterDirection.Output;

            int i = 0;
            try {
                connection.Open();
                command.ExecuteNonQuery();
                connection.Close();
            }
            catch { }
            i = Convert.ToInt32(command.Parameters["@result"].Value);
            return i;
        }

    }
}
